﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.circlereview
{
    public partial class shortmessage_create : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ChangeHope.WebPage.PageRequest.GetInt("id") > 0)
                {
                    BandInfo(ChangeHope.WebPage.PageRequest.GetInt("id"));
                }
                //回复短消息
                if (ChangeHope.WebPage.PageRequest.GetQueryString("userid")!="" && ChangeHope.WebPage.PageRequest.GetQueryString("option") == "reply")
                {
                    this.txtUserId.Text = ChangeHope.WebPage.PageRequest.GetQueryString("userid");
                }
                //转发
                if (ChangeHope.WebPage.PageRequest.GetQueryString("content") != "" && ChangeHope.WebPage.PageRequest.GetQueryString("option") == "resend")
                {
                    this.txtContent.Text = ChangeHope.WebPage.PageRequest.GetQueryString("content");
                }
                InitWebControl();
            }
        }

        #region 验证
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtUserId, "输入收件人的账号", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtTitle, "输入短消息的标题", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtContent, "输入短消息的内容", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        /// <summary>
        /// 草稿箱信息
        /// </summary>
        /// <param name="id"></param>
        protected void BandInfo(int id)
        {
            YXShop.BLL.Member.Mailinfo InfoBll = new YXShop.BLL.Member.Mailinfo();
            YXShop.Model.Member.MailInfo InfoModel = InfoBll.GetModelByID(id);
            try
            {
                if (InfoModel != null)
                {              
                    this.txtTitle.Text = InfoModel.Title;
                    this.txtContent.Text = InfoModel.Body;
                }
            }
            catch { }
            finally
            {
                InfoModel = null;
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //草稿箱
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            string UserId = this.txtUserId.Text.Trim();
            if (UserId.EndsWith(","))
            {
                UserId = UserId.Substring(0, UserId.LastIndexOf(','));
            }
            //进行拆分 给每个字段加上单引号
            string[] arrUid = UserId.Split(',');
            foreach (string s in arrUid)
            {
                if (!bll.Exists(s))
                {
                    this.ltlMsg.Text = "不存在用户：" + s;
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                SaveMessage(s);
            }
            string url = YXShop.TemplateAction.Common.UrlStr("shortmessage_list", "type=0", "membercenter/circlereview/");
            Response.Redirect(url);

        }

        protected void SaveMessage(string userId)
        {
            YXShop.BLL.Member.Mailinfo InfoBll = new YXShop.BLL.Member.Mailinfo();
            YXShop.Model.Member.MailInfo InfoModel = new YXShop.Model.Member.MailInfo();
            if (Session["MemberName"] != null)
            {
                InfoModel.Sender = Session["MemberName"].ToString();
            }
            else
            {
                ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：请重新登录！");
            }
            InfoModel.SendTime = DateTime.Now;
            InfoModel.SendIp = Request.UserHostAddress;
            InfoModel.Title = this.txtTitle.Text;
            InfoModel.Body = this.txtContent.Text;
            //未删
            InfoModel.Stat = 1;
            InfoBll.Add(InfoModel);//信息内容添加
        }

        protected void SendMessage(string userId)
        { 
            YXShop.BLL.Member.Mailinfo InfoBll = new YXShop.BLL.Member.Mailinfo();
            YXShop.Model.Member.MailInfo InfoModel = new YXShop.Model.Member.MailInfo();
            YXShop.BLL.Member.MailReceiver ReceBll = new YXShop.BLL.Member.MailReceiver();
            YXShop.Model.Member.MailReceiver ReceModel = new YXShop.Model.Member.MailReceiver();
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            if (Session["MemberName"] != null)
            {
                InfoModel.Sender = Session["MemberName"].ToString();
            }
            else
            {
                ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：请重新登录！");
            }
            InfoModel.SendTime = DateTime.Now;
            InfoModel.SendIp = Request.UserHostAddress;
            InfoModel.Title = this.txtTitle.Text;
            InfoModel.Body = this.txtContent.Text;
            //未删
            InfoModel.Stat = 0;
            InfoBll.Add(InfoModel);//信息内容添加

            ReceModel.Receiver = userId;
            ReceModel.ReceiverId = GetIdByUserId(userId);
            ReceModel.ReceiveTime = DateTime.Now;
            ReceModel.Stat = 0;
            ReceModel.IsRead = 0;
            ReceModel.Title = this.txtTitle.Text;
            ReceModel.Body = this.txtContent.Text;
            ReceModel.Sender = Session["MemberName"].ToString();
            ReceBll.Add(ReceModel);

        }

        /// <summary>
        /// 根据账号得到自增ID
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        protected int GetIdByUserId(string UserId)
        {
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            YXShop.Model.Member.MemberAccount model = bll.GetModel(UserId);
            if (model != null)
            {
                return model.UID;
            }
            else
            {
                return -1;
            }
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            YXShop.BLL.Member.MemberAccount bll = new YXShop.BLL.Member.MemberAccount();
            string UserId = this.txtUserId.Text.Trim();
            if (UserId.EndsWith(","))
            {
                UserId = UserId.Substring(0, UserId.LastIndexOf(','));
            }
            //进行拆分 给每个字段加上单引号
            string[] arrUid = UserId.Split(',');
            foreach (string s in arrUid)
            {
                if (!bll.Exists(s))
                {
                    this.ltlMsg.Text = "不存在用户：" + s;
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                //发送到对方收件箱 未读
                SendMessage(s);
            }
            string url = YXShop.TemplateAction.Common.UrlStr("shortmessage_list", "type=2", "membercenter/circlereview/");
            Response.Redirect(url);
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            this.txtTitle.Text = "";
            this.txtContent.Text = "";
            this.txtUserId.Text = "";
        }
    }
}
